﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnguaRPGEngine.DND4eModule;
using AnguaRPGEngine.Library.Systems;
using AnguaRPGEngine.Library.Parts;
using AnguaRPGEngine.Library.Interfaces;
using AnguaRPGEngine.Library.Controls;
using System.IO;

namespace AnguaRPGEngine.DND4eModule.Forms
{
    public partial class AreaEditor : Form
    {
        private Bitmap _map = new Bitmap(1, 1);
        private AdventureArea _Area = new AdventureArea();
        public AdventureArea Area
        {
            get
            {
                return _Area;
            }
            set
            {
                _Area = value;
            }
        }
        public AreaEditor()
        {
            InitializeComponent();
            _Area.AreaMapFileSpec = "";
            _Area.ExperiencePoints = 500;
            _Area.Features = new List<Feature>();
            _Area.Name = "";
            _Area.OtherInformation = "";
            _Area.QuestionsAnswers = new List<QuestionAnswer>();
            _Area.ReadAloudText = new List<string>();
            _Area.Setup = "";
            _Area.Tactics = "";
        }

        private void LoadMap()
        {
            List<IPart> map = new List<IPart>();
            MapFileClass mapFileClass = Serializer<MapFileClass>.DeserializeFromFile(txtMapFile.Text);
            foreach (Floor floor in mapFileClass.Floors)
            {
                map.Add(floor);
            }
            foreach (Door door in mapFileClass.Doors)
            {
                map.Add(door);
            }
            foreach (Scenery scenery in mapFileClass.Scenery)
            {
                map.Add(scenery);
            }
            foreach (Counter counter in mapFileClass.Counters)
            {
                map.Add(counter);
            }
            foreach (Effect effect in mapFileClass.Effects)
            {
                map.Add(effect);
            }
            foreach (Wall wall in mapFileClass.Walls)
            {
                map.Add(wall);
            }
            foreach (Markup markup in mapFileClass.Markups)
            {
                map.Add(markup);
            }
            if (map != null)
            {
                string MapName = txtMapFile.Text.Remove(0, txtMapFile.Text.LastIndexOf("\\") + 1);
                MapName = MapName.Substring(0, MapName.LastIndexOf("."));
                using (MapControl mapC = new MapControl())
                {
                    mapC.BitmapOnly = true;
                    mapC.Parts = map;
                    mapC.Size = new Size(1024, 768);
                    mapImage.Image = mapC.GenerateBitmap();
                }
            }
        }

        private void txtMapFile_TextChanged(object sender, EventArgs e)
        {
            _Area.AreaMapFileSpec = txtMapFile.Text;
            if (File.Exists(txtMapFile.Text))
            {
                LoadMap();
            }
        }

        private void AreaEditor_Load(object sender, EventArgs e)
        {
            InitializeGeneral();
            InitializeSetup();
            InitializeTactics();
            InitializeQandA();
            InitializeOtherInfo();
            InitializeReadAloudText();
            InitializeFeatures();
            LoadGeneral();
            LoadSetup();
            LoadTactics();
            LoadQandA();
            LoadOtherInfo();
            LoadReadAloudText();
            LoadFeatures();
        }
        private void InitializeGeneral()
        {
            txtMapFile.Text = "";
            txtName.Text = "";
            nudXP.Value = 500;
        }
        private void InitializeSetup()
        {
            txtSetup.Text = "";
        }
        private void InitializeTactics()
        {
            txtTactics.Text = "";
        }
        private void InitializeQandA()
        {
            lvQandAs.ListView.Columns.Clear();
            lvQandAs.OnButtonClicked += new GenericListControl.ButtonClickedEventHandler(lvQandAs_OnButtonClicked);
            AdventureArea.GetQuestionAndAnswerHeaders(lvQandAs.ListView);
        }

        void lvQandAs_OnButtonClicked(GenericListControl.GenericListButtons Button, List<int> SelectedItemIndexes)
        {
            switch (Button)
            {
                case GenericListControl.GenericListButtons.Add:
                    using (QuestionAnswerEditor qaEditor = new QuestionAnswerEditor())
                    {
                        if (qaEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            _Area.QuestionsAnswers.Add(qaEditor.QandA);
                        }
                    }
                    break;
                case GenericListControl.GenericListButtons.Edit:
                    using (QuestionAnswerEditor qaEditor = new QuestionAnswerEditor())
                    {
                        qaEditor.QandA = _Area.QuestionsAnswers[SelectedItemIndexes[0]];
                        if (qaEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            _Area.QuestionsAnswers[SelectedItemIndexes[0]] = qaEditor.QandA;
                        }
                    }
                    break;
                case GenericListControl.GenericListButtons.Delete:
                    string Display = string.Format("Are you certain that you wish to delete th{0}?",
             SelectedItemIndexes.Count > 1 ? string.Format("ese {0} question and answer pairs", SelectedItemIndexes.Count) : "is question and answer pair");
                    string Title = string.Format("Confirm{0}Deletion", SelectedItemIndexes.Count > 1 ? " Mulitple " : " ");
                    if (MessageBox.Show(this, Display, Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        for (int i = SelectedItemIndexes.Count - 1; i > -1; i--)
                        {
                            _Area.QuestionsAnswers.RemoveAt(SelectedItemIndexes[i]);
                        }
                    }
                    break;
            }
            LoadQandA();
        }
        private void InitializeOtherInfo()
        {
            txtOtherInfo.Text = "";
        }
        private void InitializeReadAloudText()
        {
            lvReadAloudTexts.ListView.Columns.Clear();
            lvReadAloudTexts.OnButtonClicked += new GenericListControl.ButtonClickedEventHandler(lvReadAloudTexts_OnButtonClicked);
            AdventureArea.GetReadAloudTextHeaders(lvReadAloudTexts.ListView);
        }

        void lvReadAloudTexts_OnButtonClicked(GenericListControl.GenericListButtons Button, List<int> SelectedItemIndexes)
        {
            switch (Button)
            {
                case GenericListControl.GenericListButtons.Add:
                    using (ReadAloudTextEditor ratEditor = new ReadAloudTextEditor())
                    {
                        if (ratEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            _Area.ReadAloudText.Add(ratEditor.ReadAloudText);
                        }
                    }
                    break;
                case GenericListControl.GenericListButtons.Edit:
                    using (ReadAloudTextEditor ratEditor = new ReadAloudTextEditor())
                    {
                        ratEditor.ReadAloudText = _Area.ReadAloudText[SelectedItemIndexes[0]];
                        if (ratEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            _Area.ReadAloudText[SelectedItemIndexes[0]] = ratEditor.ReadAloudText;
                        }
                    }
                    break;
                case GenericListControl.GenericListButtons.Delete:
                    string Display = string.Format("Are you certain that you wish to delete th{0}?",
                          SelectedItemIndexes.Count > 1 ? string.Format("ese {0} read aloud texts", SelectedItemIndexes.Count) : "is read aloud text");
                    string Title = string.Format("Confirm{0}Deletion", SelectedItemIndexes.Count > 1 ? " Mulitple " : " ");
                    if (MessageBox.Show(this, Display, Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        for (int i = SelectedItemIndexes.Count - 1; i > -1; i--)
                        {
                            _Area.ReadAloudText.RemoveAt(SelectedItemIndexes[i]);
                        }
                    }
                    break;
            }
            LoadReadAloudText();
        }
        private void InitializeFeatures()
        {
            lvFeatures.ListView.Columns.Clear();
            lvFeatures.OnButtonClicked += new GenericListControl.ButtonClickedEventHandler(lvFeatures_OnButtonClicked);
            AdventureArea.GetFeatureHeaders(lvFeatures.ListView);
        }

        void lvFeatures_OnButtonClicked(GenericListControl.GenericListButtons Button, List<int> SelectedItemIndexes)
        {
            switch (Button)
            {
                case GenericListControl.GenericListButtons.Add:
                    using (FeatureEditor fEditor = new FeatureEditor())
                    {
                        if (fEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            _Area.Features.Add(fEditor.Feature);
                        }
                    }
                    break;
                case GenericListControl.GenericListButtons.Edit:
                    using (FeatureEditor fEditor = new FeatureEditor())
                    {
                        fEditor.Feature = _Area.Features[SelectedItemIndexes[0]];
                        if (fEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            _Area.Features[SelectedItemIndexes[0]] = fEditor.Feature;
                        }
                    }
                    break;
                case GenericListControl.GenericListButtons.Delete:
                    string Display = string.Format("Are you certain that you wish to delete th{0}?",
                          SelectedItemIndexes.Count > 1 ? string.Format("ese {0} features", SelectedItemIndexes.Count) : "is feature");
                    string Title = string.Format("Confirm{0}Deletion", SelectedItemIndexes.Count > 1 ? " Mulitple " : " ");
                    if (MessageBox.Show(this, Display, Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        for (int i = SelectedItemIndexes.Count - 1; i > -1; i--)
                        {
                            _Area.Features.RemoveAt(SelectedItemIndexes[i]);
                        }
                    }
                    break;
            }
            LoadFeatures();
        }
        private void LoadGeneral()
        {
            txtName.Text = _Area.Name;
            txtMapFile.Text = _Area.AreaMapFileSpec;
            nudXP.Value = _Area.ExperiencePoints;
        }
        private void LoadSetup()
        {
            txtSetup.Text = _Area.Setup;
        }
        private void LoadTactics()
        {
            txtTactics.Text = _Area.Tactics;
        }
        private void LoadQandA()
        {
            _Area.GetQuestionAndAswerItems(lvQandAs.ListView);
        }
        private void LoadOtherInfo()
        {
            txtOtherInfo.Text = _Area.OtherInformation;
        }
        private void LoadReadAloudText()
        {
            _Area.GetReadAloudTextItems(lvReadAloudTexts.ListView);
        }
        private void LoadFeatures()
        {
            _Area.GetFeatureItems(lvFeatures.ListView);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Title = "Open Map";
                ofd.Filter = "Angua R.P.G. Engine Maps (*.rpgmap)|*.rpgmap";
                ofd.AddExtension = true;
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    txtMapFile.Text = ofd.FileName;
                }
            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            _Area.Name = txtName.Text;
        }

        private void nudXP_ValueChanged(object sender, EventArgs e)
        {
            _Area.ExperiencePoints = (int)nudXP.Value;
        }
    }
}